package logic

import (
	"context"
	"fmt"
	"house/models/cache"
	"house/models/mysql"

	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatesPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatesPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatesPasswordLogic {
	return &UpdatesPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatesPasswordLogic) UpdatesPassword(in *houserpc.UpdatesPasswordRequest) (*houserpc.UpdatesPasswordResponse, error) {
	// todo: add your logic here and delete this line
	result, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile).Result()
	if result != in.SmsCode {
		return nil, fmt.Errorf("短信验证码错误，请重新输入")
	}
	var user mysql.User
	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前手机号不存在，不允许修改")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("两次密码输入一致，不允许修改")
	}
	if in.Password == in.NewPassword {
		return nil, fmt.Errorf("两次密码输入相同，不允许修改")
	}
	in.Password = in.NewPassword
	return &houserpc.UpdatesPasswordResponse{
		UserId: int64(user.ID),
	}, nil
}
